package com.whty.cms.filewps.common.controller;

import com.whty.cms.filewps.common.message.ResponseMessage;
import com.whty.cms.filewps.util.MemcacheUtil;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * @Desc 缓存工具类使用
 * @Auth fengc
 * @Date 2019-06-13
 */
@RestController
public class CacheController {
    @PostMapping("/memcache/getKey")
    public Object getMemCacheValueByKey(String key) {
        ResponseMessage result = new ResponseMessage();
        Object obj = MemcacheUtil.getObj(key);
        if(null != obj) {
            result.setData(obj);
        }
        result.success();
        return result;
    }

    @PostMapping("/memcache/remove")
    public ResponseMessage removeMemCacheByKey(String key) {
        boolean flag = MemcacheUtil.delObj(key);
        ResponseMessage result = new ResponseMessage();
        if(flag) {
            result.setData("删除缓存 key :" + key + "成功！");
            result.success();
        } else {
            result.failed();
        }
        return result;
    }

    @PostMapping("/memcache/flushAll")
    public ResponseMessage flushMemCacheAll() {
        ResponseMessage result = new ResponseMessage();
        MemcacheUtil.flushAll();
        result.setData("清楚所有缓存成功！");
        result.success();
        return result;
    }
}
